package com.zuipin.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zuipin.entity.SysUnit;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.Result;
import com.zuipin.mapper.SysUnitMapper;
import com.zuipin.service.ISysUnitService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.vo.UnitTreeNode;

/**
 * 组织部门服务实现
 * 
 * @ClassName: SysUnitSer5viceImpl
 * @Description: TODO
 * @author  robot
 * @date 2017年09月147日 09:21:09
 */
@Service
public class SysUnitServiceImpl implements ISysUnitService {
	
	@Resource
	private SysUnitMapper sysUnitMapper;
	
	@Override
	public SysUnit findById(Integer id) {
		return sysUnitMapper.findById(id);
	}
	
	@Override
	public void insert(SysUnit sysUnit) {
		sysUnitMapper.insert(sysUnit);
	}
	
	@Override
	public void update(SysUnit sysUnit) {
		sysUnitMapper.update(sysUnit);
	}
	
	@Override
	public void delete(Integer id) {
		sysUnitMapper.delete(id);
		
	}
	
	@Override
	public List<SysUnit> seachSysUnit(SysUnit sysUnit, Pagination page) {
		return sysUnitMapper.seachSysUnit(sysUnit, page);
	}
	
	@Override
	public Result<SysUnit> saveOrUpdateUnit(SysUnit sysUnit) {
		String now = DateUtils.getCurrentDateTime();
		Result<SysUnit> result = new Result<>();
		if (sysUnit.getId() == null) {
			sysUnit.setCreateId(sysUnit.getUpdateId());
			sysUnit.setCreateTime(now);
			sysUnit.setUpdateTime(now);
			sysUnit.setDelFlag(SysUnit.DEL_NO);
			sysUnit.setUnitStatus(SysUnit.ENABLE);
			insert(sysUnit);
			
			// 填充path信息
			String parentPath = sysUnitMapper.findById(sysUnit.getParentId()).getPath();
			String path = sysUnit.getId() + "/" + parentPath;
			sysUnit.setPath(path);
			updateSysUnit(sysUnit);
			result.setData(sysUnit);
		} else {
			sysUnit.setUpdateTime(now);
			updateSysUnit(sysUnit);
		}
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}
	
	/**
	 * 加载组织 树列表
	 * @author zhuangjiajian
	 * @date 2017-9-23 14:34:35
	 * 
	 */
	@Override
	public List<UnitTreeNode> getSysUnitTree(Integer unitStatus) {
		
		List<UnitTreeNode> treeNodes = new ArrayList<>();
		// 构建头部节点
		SysUnit sysUnit = sysUnitMapper.findById(UnitTreeNode.DEFAULT_HEAD_NODE);
		UnitTreeNode headNode = new UnitTreeNode(sysUnit);
		// 获取第一级部分列表
		List<UnitTreeNode> childUnits = initUnitTree(UnitTreeNode.DEFAULT_HEAD_NODE, unitStatus);
		headNode.setChildUnits(childUnits);
		treeNodes.add(headNode);
		return treeNodes;
	}
	
	/**
	 * 加载树型组织列表
	 * @author zhuangjiajian
	 * @date 2017-9-23 15:09:17
	 * 
	 */
	private List<UnitTreeNode> initUnitTree(Integer unitId, Integer unitStatus) {
		List<SysUnit> childUnits = sysUnitMapper.queryUnitByParent(unitId, unitStatus);
		if (childUnits != null && childUnits.size() > 0) {
			List<UnitTreeNode> childNodes = new ArrayList<>();
			for (SysUnit sysUnit : childUnits) {
				UnitTreeNode unitTreeNode = new UnitTreeNode(sysUnit);
				List<UnitTreeNode> grandchilds = initUnitTree(sysUnit.getId(), unitStatus);
				unitTreeNode.setChildUnits(grandchilds);
				childNodes.add(unitTreeNode);
			}
			return childNodes;
		}
		return null;
	}
	
	@Override
	public List<SysUnit> searchSysUnit(SysUnit sysUnit, Pagination page) {
		return sysUnitMapper.searchSysUnit(sysUnit, page);
	}
	
	@Override
	public int updateSysUnit(SysUnit sysUnit) {
		// TODO Auto-generated method stub
		return sysUnitMapper.updateSysUnit(sysUnit);
	}
	
}
